//
//  GroupMemberController.m
//  EducationAdminClient
//
//  Created by 孙添凤 on 17/2/8.
//  Copyright © 2017年 周佳兴. All rights reserved.
//

#import "GroupMemberController.h"
#import "MemberCell.h"
#import "NetRequest.h"
@interface GroupMemberController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>{
    UICollectionView *collectionView;
    UILabel *la;
    
    UILabel *allLable;
    UILabel *n;
}
@property(nonatomic,strong)NSMutableArray *mutArr;
@end

@implementation GroupMemberController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _mutArr =[NSMutableArray array];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((KWIDTH/5)-10, KWIDTH/5-10);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 0;
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 300) collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    
    [collectionView registerClass:[MemberCell class] forCellWithReuseIdentifier:@"cellId"];
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Refuse"];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    
    //4.设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self datarequest];
    
    allLable =[[UILabel alloc]initWithFrame:CGRectMake(KWIDTH-200, 300, 200, 20)];
    [self.view addSubview:allLable];
    allLable.text = @"全部成员10人";
    allLable.font =[UIFont systemFontOfSize:14];
    allLable.textAlignment = NSTextAlignmentRight;
    
    n =[[UILabel alloc]initWithFrame:CGRectMake(10, 300, 300, 20)];
    [self.view addSubview:n];
    n.text = @"群聊名称";
    n.font =[UIFont systemFontOfSize:14];
    
    
    
    
}

-(void)datarequest{
    NSMutableDictionary *param =[NSMutableDictionary dictionary];
    [param setObject:self.channalID forKey:@"mainId"];
    [param setObject:@"17797" forKey:@"tableId"];
    [param setObject:@"17796" forKey:@"mainTableId"];
    [param setObject:@"7686" forKey:@"mainPageId"];
    [param setObject:@"7690" forKey:@"pageId"];
    NSString *requestUrl =[NSString stringWithFormat:@"%@",memberUrl];
    [NetRequest requestUrl:requestUrl andParamas:param andReturnBlock:^(NSData *data, NSError *error) {
        if(!error&&data!=nil){
            NSDictionary *obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSLog(@"%@",obj);
            _mutArr  = [obj objectForKey:@"dataList"];
            allLable.text =[NSString stringWithFormat:@"全部群成员(%lu)人",(unsigned long)_mutArr.count];
            n.text =[NSString stringWithFormat:@"群聊名称:%@",self.customName];
            [collectionView reloadData];

        }
    }];
}

- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    
    if (kind == UICollectionElementKindSectionHeader)
    {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Refuse" forIndexPath:indexPath];
        la =[[UILabel alloc]initWithFrame:CGRectMake(KWIDTH/2-50, 5, 100, 20)];
        la.backgroundColor =[UIColor cyanColor];
        [headerView addSubview:la];
        la.font = [UIFont systemFontOfSize:13];
        if(indexPath.section==0){
           la.text = @"教师";
        }else{
          la.text = @"学生";
        }
        
        la.textAlignment = NSTextAlignmentCenter;
        
        reusableview = headerView;
    }
    
    
    reusableview.backgroundColor = [UIColor cyanColor];
    
    return reusableview;
}

#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if(section==0){
      return _mutArr.count;
    }else{
      return _mutArr.count;
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    MemberCell *cell = (MemberCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    NSDictionary *dic = [_mutArr objectAtIndex:indexPath.row];
    if(indexPath.section==0){
        cell.botlabel.text = [dic objectForKey:@"USERNAME"];
    }else{
      cell.botlabel.text = [dic objectForKey:@"STUNAME"];
    }
    
    
    cell.backgroundColor = [UIColor yellowColor];
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
